<?php

namespace App\Services\Tools;

use App\Services\Gongmall\UserSign;
use App\Services\Gongmall\UserWithdrawals;
use App\Exceptions\BasicException;

/***
 * 用户关于工猫的交互,签约提现等操作
 */
class UserGongMallService
{
    // 提现状态转换
    public $payStatus = [
        10 => 0, // 处理中->待付款
        20 => 1, // 成功->已付款
        30 => 2 // 失败
    ];

    public function autoSign($params)
    {
        $res = $this->sign($params);
        if (!$res['success']) {
            return $res;
        }

        $confirm_sign_params = [
            'mobile' => $params['mobile'],
            'contractId' => $res['data']['contractId'],
        ];
        $res = self::confirmSign($confirm_sign_params);
        return $res;

    }

    public static function getContractList()
    {
        $gongmall = new UserSign();
        return $gongmall->getContractList();
    }
    // 用户发起签约，签约模板用默认的
    public function sign($params)
    {
        $check_params = [
            'name' => '名称必传',
            'mobile' => '手机号必传',
            'certificateType' => '证件类型必传',
            'identity' => '证件号码必传',
        ];
        $this->validate_params($check_params, $params);
        $gongmall = new UserSign();
        $res = $gongmall->sign($params);
        gongmallLog("gongmall:发起签约响应" . json_encode($res, JSON_UNESCAPED_UNICODE));
        if (!$res['success']) {
            // 失败 记录日志
            gongmallLog('gongmall:' . json_encode($res, JSON_UNESCAPED_UNICODE));
            return ['success' => false, 'msg' => $res['errorMsg']];
        }
        return $res;
    }


    public function getContractStatusById($params)
    {
        if (!$params['id']) {
            throw new BasicException('-1', '缺少银行id');
        }
        if (!$params['identity']) {
            throw new BasicException('-1', '身份证号必传');
        }
        if (!$params['contractId']) {
            throw new BasicException('-1', '签约contractId必传');
        }
        $gongmall = new UserSign();
        $res = $gongmall->getContractStatusByContractId($params);
        // 根据合同id更新状态
        if ($res['success']) {
            $update = [];
            $update['process_status'] = $res['data']['processStatus'];
            if ((int)$res['data']['processStatus'] === 3) {
                $update['status'] = 1; // 银行卡状态更新为审核通过
            }
            app('model_user_bank')->where(['id' => $params['id']])->update($update);
        }else{
            throw new BasicException('-1', $res['errorMsg']);
        }
        return $res;
    }


    // 确认签约
    public function confirmSign($params)
    {
        $data = [];
        $check_params = [
            'mobile' => '手机号必传',
            'contractId' => '电签合同id必传',
        ];
        $this->validate_params($check_params, $params);
        $data['mobile'] = $params['mobile'];
        $data['contractId'] = $params['contractId'];
        $gongmall = new UserSign();
        $res = $gongmall->confirmSign($data);
        gongmallLog("gongmall:确认签约响应" . json_encode($res, JSON_UNESCAPED_UNICODE));
        if (!$res['success']) {
            // 失败 记录日志
            gongmallLog('gongmall:' . json_encode($res, JSON_UNESCAPED_UNICODE));
            return ['success' => false, 'msg' => $res['errorMsg']];
        }
        return ['success' => true, 'msg' => $res['errorMsg']];

    }

    public function validate_params($check_params, $params)
    {
        foreach ($check_params as $k => $v) {
            if (empty($params[$k])) {
                throw new BasicException('-1', $v);
            }
        }
    }

}
